package st.business.tepth.business.taskallocation.returnfactory.content;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RadioGroup;

import butterknife.BindView;
import st.business.tepth.R;
import st.business.tepth.R2;
import st.business.tepth.base.OnItemChooseListener;
import st.business.tepth.business.bean.ReturnFactoryBean;
import st.shadow.library.core.base.mvp.MvpFragment;

/**
 * Author: shadow
 * Email:  1025431817@qq.com
 * Time:   2019-09-03 10:04
 */

public class ContentDelegate extends MvpFragment<ContentPresenter> implements ContentContract.View {
    public static ContentDelegate newInstance(ReturnFactoryBean.Classification
                                                      classification) {
        Bundle bundle = new Bundle();
        bundle.putParcelable("classification", classification);
        ContentDelegate delegate = new ContentDelegate();
        delegate.setArguments(bundle);
        return delegate;
    }

    @BindView(R2.id.radio_group)
    RadioGroup mRadioGroup;
    @BindView(R2.id.recycler_view_maintain)
    RecyclerView mRecyclerViewMaintain;
    @BindView(R2.id.recycler_view_repair)
    RecyclerView mRecyclerViewRepair;
    private ReturnFactoryBean.Classification mClassification;
    private MaintainAdapter mMaintainAdapter;
    private RepairAdapter mRepairAdapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mClassification = bundle.getParcelable("classification");
        }
    }

    @Override
    public ContentPresenter getPresenter() {
        return new ContentPresenter(this);
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_return_facotry_content;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb_maintain) {
                    showMaintain();
                } else {
                    showRepair();
                }
            }
        });
        mRecyclerViewMaintain.setLayoutManager(new LinearLayoutManager(_mActivity));
        RecyclerView.ItemAnimator itemAnimator = mRecyclerViewMaintain.getItemAnimator();
        if (itemAnimator != null) {
            itemAnimator.setChangeDuration(0);
        }
        mMaintainAdapter = new MaintainAdapter(_mActivity,
                MaintainAdapter.convertData(mClassification), new OnItemChooseListener<Boolean>() {
            @Override
            public void onChoose(Boolean aBoolean) {

            }
        });
        mRecyclerViewMaintain.setAdapter(mMaintainAdapter);

        mRecyclerViewRepair.setLayoutManager(new LinearLayoutManager(_mActivity));
        RecyclerView.ItemAnimator itemAnimatorRepair = mRecyclerViewMaintain.getItemAnimator();
        if (itemAnimatorRepair != null) {
            itemAnimatorRepair.setChangeDuration(0);
        }
        mRepairAdapter = new RepairAdapter(R.layout.item_maintain_work,
                mClassification.getRepairPhenomenonList());

        mRecyclerViewRepair.setAdapter(mRepairAdapter);
        if (mMaintainAdapter.getData().size() == 1) {
            mMaintainAdapter.expand(0);
        }
        showMaintain();
    }

    private void showMaintain() {
        mRecyclerViewMaintain.setVisibility(View.VISIBLE);
        mRecyclerViewRepair.setVisibility(View.GONE);
    }

    private void showRepair() {
        mRecyclerViewMaintain.setVisibility(View.GONE);
        mRecyclerViewRepair.setVisibility(View.VISIBLE);
    }
}